package com.hermes.action.login;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.hermes.biz.common.util.login.CorpLoginUtil;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.common.util.ParameterParserUtil;

public class LoginCorpComTest {

	private static CorpLoginUtil clu = new CorpLoginUtil();
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			execute2();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

public static String execute() throws IOException {
		
		String codeRes = "16717dd3bb66e23b199dbabe02e84fc7";

		if(StringUtil.isNotBlank(codeRes)){
			String getATRes = executeSendPost(clu.sinaGetAccessTokenUrl,"&code="+codeRes);
			JSONObject jo = JSONObject.fromObject(getATRes);
			String atokenStr = jo.getString("access_token");
			String uidStr = jo.getString("uid");
			String getUserParamStr = "access_token="+atokenStr+"&uid="+uidStr;
			String userInfoJsonStr = executeSend(clu.getSinaUserInfoUrl,getUserParamStr);
			JSONObject userInfo = JSONObject.fromObject(userInfoJsonStr);
			String userName = userInfo.getString("screen_name");
			 System.out.println(userName);  
		}
		else{
			//进这里是傻逼
		}
		return null;
	}
	
	public static String executeSend(String url ,String param){
		try{
			String sendURL = url+param;
			HttpClient hc = new DefaultHttpClient();
			HttpGet httpget= new HttpGet(sendURL);  
			HttpResponse response =hc.execute(httpget);
	        HttpEntity entity = response.getEntity();  
	        System.out.println("<< Response: " + response.getStatusLine());  

	        return EntityUtils.toString(entity);
		}
		catch(Exception e){
			
		}
		return "";

	}

	public static String executeSendPost(String url ,String param){
		try{
			String sendURL = url+param;
			HttpClient hc = new DefaultHttpClient();
			HttpPost httpget= new HttpPost(sendURL);  
			HttpResponse response =hc.execute(httpget);
	        HttpEntity entity = response.getEntity();  
	        System.out.println("<< Response: " + response.getStatusLine());  

	        return EntityUtils.toString(entity);
		}
		catch(Exception e){
			
		}
		return "";

	}
	
public static String execute2() throws IOException {
		
		String codeRes = "4B4284DB5835B03571637804A181A289";

		if(StringUtil.isNotBlank(codeRes)){
			String getATRes = executeSend(clu.qqGetAccessTokenUrl,"code="+codeRes);
			 System.out.println(getATRes);
			String[] resArray = getATRes.split("&");
			String getOpenIdStr = executeSend(clu.qqGetOpenIdUrl,resArray[0]);
			 System.out.println(getOpenIdStr);
			getOpenIdStr = getOpenIdStr.substring(9, getOpenIdStr.length()-3);
			System.out.println(getOpenIdStr);
			JSONObject jo = JSONObject.fromObject(getOpenIdStr);
			String openId = jo.getString("openid");
			String getUserParamStr = "access_token="+resArray[0]+"&oauth_consumer_key=100316154&openid="+openId;
			String userInfoJsonStr = executeSend(clu.qqGetUserInfoUrl,getUserParamStr);
			  System.out.println(userInfoJsonStr);  
			JSONObject userInfo = JSONObject.fromObject(userInfoJsonStr);
			String userName = userInfo.getString("nickname");
	        System.out.println(userName);  

		}
		else{
			//进这里是傻逼
		}
		return null;
	}
	
	
}
